package a3;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Vector;


	public class DrawingBoard extends Canvas {

		/**
		 * 
		 */
		private static final long serialVersionUID = 18217624658141818L;
		Gui GUI;
	    //    class variables
	    private int bufferWidth;
	    private int bufferHeight;
	    private Image bufferImage;
	    private Graphics bufferGraphics;
		
		
		public DrawingBoard(Gui gui) {
			GUI = gui;
			setSize(GUI.getWidth(), GUI.getHeight());
		}
		
		public void update(Graphics g) {
			paint(g);
		}
		
		public void paint(Graphics g) {
		       //    checks the buffersize with the current panelsize
	        //    or initialises the image with the first paint
	        if(bufferWidth!=getSize().width || 
	          bufferHeight!=getSize().height || 
	          bufferImage==null || bufferGraphics==null)
	            resetBuffer();

	        if(bufferGraphics!=null){
	            //this clears the offscreen image, not the onscreen one
	            bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);

	            //calls the paintbuffer method with 
	            //the offscreen graphics as a param
	            paintBuffer(bufferGraphics);

	            //we finaly paint the offscreen image onto the onscreen image
	            g.drawImage(bufferImage,0,0,this);
	        }
		}
		
	    private void resetBuffer(){
	        // always keep track of the image size
	        bufferWidth=getSize().width;
	        bufferHeight=getSize().height;

	        //    clean up the previous image
	        if(bufferGraphics!=null){
	            bufferGraphics.dispose();
	            bufferGraphics=null;
	        }
	        if(bufferImage!=null){
	            bufferImage.flush();
	            bufferImage=null;
	        }
	        System.gc();

	        //    create the new image with the size of the panel
	        bufferImage=createImage(bufferWidth,bufferHeight);
	        bufferGraphics=bufferImage.getGraphics();
	    }
	    
	    
		public void paintBuffer(Graphics g) {
			Dimension size = getSize();
			int widthStep = size.width/GUI.Environment.BoardSize;
	        int heightStep = size.height/GUI.Environment.BoardSize;
	        
		    Graphics2D g2d = (Graphics2D) g;
		    g2d.setColor(new Color(0,0,0));
		    g2d.clearRect(0, 0, size.width, size.height);
		    for(int i=0; i <= GUI.Environment.BoardSize;i++) {
		    	g2d.drawLine(0, heightStep*i, size.width, heightStep*i);
		    	g2d.drawLine(widthStep*i, 0, widthStep*i, size.height);
		    }
		    
		    g2d.setColor(new Color(255,0,0));
		    Vector<Predator> a = Environment.getPredators();
		    for(Predator p: a)
		    {
		    Location pa = p.getLocation();
		    g2d.fillRect(pa.x*widthStep+1, pa.y*heightStep+1, widthStep-1, heightStep-1);
		    }
		    g2d.setColor(new Color(0,255,0));
		   	Agent a2 =  GUI.Environment.getPrey();
		    Location pa2 = a2.getLocation();
		    g2d.fillRect(pa2.x*widthStep+1, pa2.y*heightStep+1, widthStep-1, heightStep-1);
		   
		}

	}
